//
//  Photo.swift
//  googlypuff
//
//  Created by Open on 2/4/15.
//  Copyright (c) 2015 open-groupe. All rights reserved.
//

import UIKit
import AssetsLibrary


typealias PhotoDownloadCompletionBlock = (image: UIImage?, error: NSError?) -> Void
typealias PhotoDownloadProgressBlock = (completed: Int, total: Int) ->Void

enum PhotoStatus
{
    case Downloading
    case GoodToGo
    case Failed
}

protocol Photo
{
    var status: PhotoStatus {get}
    var image:  UIImage? {get}
    var thumbnail: UIImage? {get}
}

class AssetPhoto: Photo {
    var status : PhotoStatus {
        return .GoodToGo
    }
        
    var image: UIImage? {
        let representation = asset.defaultRepresentation()
        return UIImage(CGImage: representation.fullScreenImage().takeUnretainedValue())
    }
    var thumbnail: UIImage? {
        return UIImage(CGImage: asset.thumbnail().takeUnretainedValue())
    }
    
    let asset: ALAsset
    init(asset: ALAsset)
    {
        self.asset = asset
    }
}

private let downloadSession = NSURLSession(configuration: NSURLSessionConfiguration.ephemeralSessionConfiguration())

class DownloadPhoto: Photo
{
    var status: PhotoStatus = .Downloading
    var image: UIImage?
    var thumbnail: UIImage?
    
    let url: NSURL
    init(url: NSURL,completion: PhotoDownloadCompletionBlock!)
    {
        self.url = url;
        downloadImage(completion)
        
    }
    convenience init(url: NSURL)
    {
        self.init(url: url, completion: nil)
    }
    
    func downloadImage(completion: PhotoDownloadCompletionBlock?)
    {
        let task = downloadSession.dataTaskWithURL(url, completionHandler: {
            data, response, error  in
            self.image = UIImage(data: data)
            if(error == nil && self.image != nil) {
                self.status = .GoodToGo
                
            }else {
                self.status = .Failed
            }
            self.thumbnail = self.image?.thumbnailImage(64,
                transparentBorder: 0,
                cornerRadius: 0,
                interpolationQuality: kCGInterpolationDefault)
            if let completion = completion {
                completion(image: self.image, error: error)
            }
            dispatch_async(dispatch_get_main_queue(), {
            NSNotificationCenter.defaultCenter().postNotificationName(PhotoManagerContentUpdateNotification, object: nil)
            })
        })
        
        task.resume()
    }
}









